package com.sinodata.bsm.cicp.finder.linux;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * 
 * <p>
 * Description: 交换分区发现程序
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:20:57   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class LinuxSwap extends LinuxFinder {

    private static final Logger logger = Logger.getLogger(LinuxSwap.class);
    private static final int K = 1024;
    private static String CMDSTR = "free |grep ^Swap";

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] swapInfo = null;
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            String strLine = reader.readLine();
            String[] ss = null;
            if (strLine != null) {
                ss = strLine.split("\\s+");
            }
            if (ss.length == 4) {
                swapInfo = new String[4];
                list.add(swapInfo);
                swapInfo[0] = "0";
                swapInfo[1] = "Swap";
                swapInfo[2] = getHostName(ip) + " Swap";
                swapInfo[3] = Integer.parseInt(ss[1]) / K + "";//交换分区大小,单位是MB
            }
            return list;
        } catch (IOException e) {
            logger.error("fialed to find linux swap", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("fialed to close reader", e);
                }
            }
        }
        return null;
    }
}
